﻿@{
    Layout = "";
}


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title> 滑块验证码自动识别</title>
    <script src="~/js/sekiro_web_client.js"></script>
    <script>
        function guid() {

            function S4() {
                return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            }

            return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
        }

        var client = new SekiroClient("ws://139.9.145.86:5603/websocket?group=ws-group-cailaoban&clientId=" + guid());

    </script>
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <link type="text/css" href="//g.alicdn.com/sd/ncpc/nc.css?t=1520579483" rel="stylesheet" disabled="">
    <script type="text/javascript" src="//g.alicdn.com/sd/ncpc/nc.js?t=1520579483"></script>


</head>
<body>
    <div id="dom_id_two" class="nc-container" data-nc-idx="1">
    </div>
</body>



</html>

<script>


    var startAutoSlide = true;



    $(document).ready(function () {

        //console.log("注册qcc_slide......");
        client.registerAction("qcc_slide", function (request, resolve, reject) {
            getAliCaptcha('two', request, resolve);
        });

        //console.log("注册qcc_slide完成！");
    });

    function getAliCaptcha(num, request, resolve) {
        var renderDom = '#dom_id_' + num;
        var csessionidDom = 'csessionid_' + num;
        var sigDom = 'sig_' + num;
        var tokenDom = 'token_' + num;
        var sceneDom = 'scene_' + num;
        var nc = new noCaptcha();
        var nc_appkey = 'QNYX'; // 应用标识,不可更改
        var nc_scene = 'login'; //场景,不可更改
        var nc_token = [nc_appkey, (new Date()).getTime(), Math.random()].join(':');
        var nc_option = {
            renderTo: renderDom,
            appkey: nc_appkey,
            scene: nc_scene,
            token: nc_token,
            callback: function (data) {


                //var phone_num = request['phone_num'];
                //var password = request['password'];


                let param = {
                    //"phone_prefix":"+86",
                    //"nameNormal":phone_num,
                    //"pwdNormal":password,
                    "keep": "option1",
                    "csessionid_one": data.csessionid,
                    "sig_one": data.sig,
                    "token_one": nc_token,
                    "scene_one": nc_scene,
                    "verify_type": 1
                };



                let data_ = JSON.stringify(param);
                clearInterval(mousemove_interval);
                try {
                    SetResult(data_);
                } catch (e) {

                    setTimeout(function () {
                        document.location.reload();
                    }, 100);

                    resolve(data_);
                    console.log(data_);

                    //alert(data_);
                }

                // document.getElementById(csessionidDom).value = data.csessionid;
                // document.getElementById(sigDom).value = data.sig;
                // document.getElementById(tokenDom).value = nc_token;
                // document.getElementById(sceneDom).value = nc_scene;
                // if (num == 'one') {
                // 	// if($("input[name=nameNormal]").val() && $("input[name=pwdNormal]").val()){
                // 	//     $('#user_login_normal').submit();
                // 	// }
                // } else if (num == 'two') {
                // 	if ($("input[name=nameVerify]").val()) {
                // 		$(".get-mobile-code").trigger('click');
                // 	}
                // }
            }
        };
        nc.init(nc_option);



        var retry_times = 0;
        var max_retry_times = 2;
        var mousemove_interval = undefined;

        const sleep = time => new Promise(resolve => {
            setTimeout(resolve, time);
        });

        async function detectSlide(func1, func2) {
            try {
                await sleep(100);
                for (var j = 0; j < 100; j++) {
                    if (document.querySelector(".errloading") != null) {
                        retry_times = retry_times + 1;
                        if (retry_times < max_retry_times) {
                            clearInterval(mousemove_interval);
                            await sleep(500);
                            __nc.reset();
                            await sleep(500);
                            func2(func1());
                        } else {

                            if (startAutoSlide) {

                                try {
                                    CloseForm();
                                } catch (e) {
                                    alert('滑块失败次数太多！');
                                }

                            }
                        }
                        break;
                    } else {

                        if (document.querySelector(".btn_ok")) //说明验证OK
                        {
                            break;

                        } else {

                            await sleep(100);
                        }


                    }
                }


            } catch (e) {
                //TODO handle the exception
                console.log(e);
            }


        }

        function loadSlide() {
            let pm = new Promise(async function (resolve, reject) {
                try {
                    function mousemove() {

                        if (retry_times > max_retry_times) {
                            clearInterval(mousemove_interval);
                            return;
                        }

                        var mousemove_event = document.createEvent('MouseEvents');
                        //mousemove_event.initMouseEvent('click',true,true,window,1,12,345,7,220,false,false,true,false,0,null);
                        mousemove_event.initMouseEvent('mousemove', true, true, window, parseInt(Math.random() * 5), parseInt(Math
                            .random() * 15), parseInt(Math.random() * 100), parseInt(Math.random() * 10),
                            parseInt(Math.random() * 100), false, false, true, false, 0, null);

                        document.querySelector("body").dispatchEvent(mousemove_event);






                    }

                    function mouseclick() {

                        if (retry_times > max_retry_times) {
                            clearInterval(mouseclick_interval);
                            return;
                        }

                        var mouseclick_event = document.createEvent('MouseEvents');
                        mouseclick_event.initMouseEvent('click', true, true, window, 1, parseInt(Math.random() * 12), parseInt(
                            Math.random() * 200), parseInt(Math.random() * 7), parseInt(Math.random() * 220), false, false, true,
                            false, 0, null);
                        document.querySelector("body").dispatchEvent(mouseclick_event);
                    }

                    await sleep(100);
                    for (var j = 0; j < 100; j++) {

                        if (!document.querySelector("#nc_1_n1z")) {
                            await sleep(100);
                        } else {
                            break;
                        }
                    }

                    try {

                        mousemove();
                        mouseclick();
                        var mousemove_interval = setInterval(mousemove, 200);
                        var mouseclick_interval = setInterval(mouseclick, parseInt(Math.random() * 200));


                        //await sleep(100);
                    } catch (e) {

                    }
                } catch (e) {
                    //TODO handle the exception
                    console.log(e);
                }
                resolve('成功');
            });

            return pm;
        }


        function dragSlide(promise) {
            promise.then(async function (data) {
                var event = document.createEvent('MouseEvents');
                event.initEvent('mousedown', true, false);
                document.querySelector("#nc_1_n1z").dispatchEvent(event);
                event = document.createEvent('MouseEvents');
                event.initEvent('mousemove', true, true);
                Object.defineProperty(event, 'clientX', {
                    get() {
                        return 260;
                    }
                });

                document.querySelector("#nc_1_n1z").dispatchEvent(event);

            }, (err) => {
                console.log('失败' + err)

            }).then(function (data) { //拖动滑块之后
                detectSlide(loadSlide, dragSlide);
            }, function (error) {

            });
        }

        if (startAutoSlide) {
            let prepare = loadSlide();
            dragSlide(prepare);
        }



    }


</script>
